package com.archgeek.rpc.thrfit;

import com.archgeek.rpc.thrfit.gen.StudentService;
import com.archgeek.rpc.thrfit.impl.StudentServiceImpl;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.server.TServer;
import org.apache.thrift.server.TThreadPoolServer;
import org.apache.thrift.transport.TServerSocket;

import java.net.InetSocketAddress;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author pizhihui
 * @date 2022-04-06 21:03
 */
public class ThriftServer {

    public static void main(String[] args) {

        try {

            InetSocketAddress socketAddress = new InetSocketAddress("127.0.0.1", 8732);
            TServerSocket serverSocket = new TServerSocket(socketAddress);

            ThreadPoolExecutor executor = new ThreadPoolExecutor(
                    10,
                    Integer.MAX_VALUE,
                    60,
                    TimeUnit.SECONDS,
                    new SynchronousQueue<>(),
                    r -> {
                        Thread th = new Thread(r);
                        th.setDaemon(false);
                        th.setName("Interpreter-Handler-Pool-" + th.getId());
                        return th;
                    }
            );

            TServer server = new TThreadPoolServer(new TThreadPoolServer
                    .Args(serverSocket)
                    .executorService(executor)
                    .protocolFactory(new TBinaryProtocol.Factory())
                    .processor(new StudentService.Processor<StudentService.Iface>(new StudentServiceImpl())));
            server.serve();

        } catch (Exception e) {
            e.printStackTrace();
        }


    }

}
